-- This turns on checks for common mistakes that can save you a
-- lot of time debugging. It takes some processor time, so it's
-- a good idea to turn this off when you're ready to release your app.
STRICT = true

-- This turns on the debug console, which can be brought up at
-- any point by pressing the Tab key. From the console, you can
-- watch values and run any Lua statement. The console also appears
-- automatically if your app crashes. Finally, you can press
-- Control-Alt-R (Control-Option-R on a Mac) to instantly reload your
-- code from on disk. Just like STRICT, you'd want to turn this off
-- for release.
DEBUG = true

require 'zoetrope'



bg = love.graphics.newImage( 'background.PNG' );


pinLocs =
{
	{ x = 160, y = 500 },
	{ x = 240, y = 500 },
	{ x = 320, y = 500 },
	{ x = 400, y = 500 },
	{ x = 480, y = 500 },
	{ x = 560, y = 500 },
	{ x = 640, y = 500 },

	{ x = 200, y = 400 },
	{ x = 280, y = 400 },
	{ x = 360, y = 400 },
	{ x = 440, y = 400 },
	{ x = 520, y = 400 },
	{ x = 600, y = 400 },

}

-- flea circus

-- Constant: WALKING
WALKING = "walking"

-- Constant: JUMPING
JUMPING = "jumping"



Player = Animation:extend
{
	width = 48,
	height = 48,

	-- Property: bbox
	-- bbox is set in the constructor, it seems if we initialize it to {} then
	-- the constructor doesn't override {}


	image = 'flea-player.png',
	direction = RIGHT,
	motion = WALKING,
	vover = 0,
	toclone = false,

	platform = { left = 0, right = 800 },

	sequences =
	{
		walkright = { frames = {5,6}, fps = 3 },
		walkleft = { frames = {1,2}, fps = 3 },
		jumpleft = { frames = {3,4}, fps = 3 },
		jumpright = { frames = {7,8}, fps = 3 }
	},


	new = function (self, obj)
		obj = obj or {}
		self:extend(obj)
		if obj.onNew then obj:onNew() end
		return obj
	end,



	walkLeft = function (self)
		self:play('walkleft')
		self.velocity.x = -200
		self.acceleration.x = 1
		self.direction = LEFT
		self.paused = false
	end,

	walkRight = function (self)
		self:play('walkright')
		self.velocity.x = 200
		self.acceleration.x = -1
		self.direction = RIGHT
		self.paused = false
	end,


	changeDirectionX = function ( self )
		if self.direction == RIGHT then
			self.direction = LEFT
			if self.motion == WALKING then
				self:play('walkleft')
			else
				self:play('jumpleft')
			end
			self.velocity.x = - (self.velocity.x)
			self.acceleration.x = 1
		else
			self.direction = RIGHT
			if self.motion == WALKING then
				self:play('walkright')
			else
				self:play('jumpright')
			end
			self.velocity.x = math.abs(self.velocity.x)
			self.acceleration.x = -1
		end
	end,

	jumpAction = function (self)
		if self.direction == LEFT then
			self:play('jumpleft')
		else
			self:play('jumpright')
		end
		playSound('jump.ogg')
		self.motion = JUMPING
		self.velocity.y = -250
		self.acceleration.y = 150
	end,



	onUpdate = function (self)
		if self.x < 0 then
			self.x = 0
			self:changeDirectionX()
		elseif self.x > 800 - 48 then
			self.x = 800 - 48
			self:changeDirectionX()
		end

		if self.y > 600 - 48 then
			self.y = 600 - 48
			self.motion = WALKING
			self.acceleration.y = 0
			self.velocity.y = 0
		end

		if the.keys:pressed(" ") and self.motion == WALKING then
			self:jumpAction()
		end
	end,

    onCollide = function (self, other, hOverlap, vOverlap)


	end,


}



Pin = Tile:extend
{
	width = 20,
	height = 20,
	image = 'pin.png',

	onCollide = function (self, other)
		self:displace(other)
	end,

}



the.app = App:new
{
	onRun = function (self)
		self:add(Tile:new{ image = 'background.png' })
		self.pins = Group:new()

		for _, row in pairs(pinLocs) do
			self.pins:add(Pin:new({ x = row.x, y = row.y }))
		end

		self:add(self.pins)

		self.player = Player:new({ x = 0, y = 600 - 48 })
		self.player:walkRight()
		self:add(self.player)
	end,

	onUpdate = function (self)
		-- self.debugBanner.text = self.player.motion .. self.player.velocity.y
		self.pins:collide(self.player)
    end
}
